package net.expectx.gw.service.impl;

import net.expectx.common.base.BaseConstants;
import net.expectx.common.db.DataSourceEnum;
import net.expectx.common.db.DynamicDataSource;
import net.expectx.gw.aliyun.AliyunDysmsUtil;
import net.expectx.gw.mapper.ThirdSmsCodeMapper;
import net.expectx.gw.bean.ThirdSmsCode;
import net.expectx.gw.util.AdminConstants;
import net.expectx.gw.util.AdminUtil;
import net.expectx.gw.service.ThirdSmsCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ThirdSmsCodeService实现
 * @author lijian
 * @date 2019/1/18
 */
@Service
public class ThirdSmsCodeServiceImpl extends BaseServiceImpl<ThirdSmsCode> implements ThirdSmsCodeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThirdSmsCodeServiceImpl.class);

    @Autowired
    ThirdSmsCodeMapper thirdSmsCodeMapper;
    @Override
    public Map<String, Object> getVerificationCode(String codeDevice, Byte codeType, int expiryTime) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        Map<String, Object> map = new HashMap<String, Object>(2);
        String msg = "";
        boolean flag = false;
        try {
            List<ThirdSmsCode> smsCodes=thirdSmsCodeMapper.findByAttr("code_device='"+codeDevice+"' and is_used=0 and expiry_time > "+System.currentTimeMillis(),"id desc");
            if (smsCodes.isEmpty()){
                if (1==codeType){
                    int code = AdminUtil.randomSmsCode(6);
                    Map<String,String>paramsMap=new HashMap<String, String>();
                    AliyunDysmsUtil.config("唐豆健康家园","SMS_157070039");
                    paramsMap.put("code",String.valueOf(code));
                    map= AliyunDysmsUtil.sendSms(codeDevice,paramsMap);
                  //map.put("flag",true);
                    if(Boolean.valueOf(map.get("flag").toString())){
                        DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
                        ThirdSmsCode thirdSmsCode=new ThirdSmsCode();
                        thirdSmsCode.setCode(String.valueOf(code));
                        thirdSmsCode.setReferenceCode(System.currentTimeMillis());
                        thirdSmsCode.setExpiryTime(System.currentTimeMillis()+expiryTime*60*1000);
                        thirdSmsCode.setGmtCreate(new Date());
                        thirdSmsCode.setIsDeleted(BaseConstants.NO);
                        thirdSmsCode.setIsUsed(BaseConstants.NO);
                        thirdSmsCode.setCodeDevice(codeDevice);
                        thirdSmsCode.setGmtModified(new Date());
                        thirdSmsCodeMapper.insertSelective(thirdSmsCode);
                        map.put("referenceCode",thirdSmsCode.getReferenceCode());
                        flag = true;
                    }
                }
            }else{
                msg="您已获得验证码，有效期为"+expiryTime+"分钟";
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getLocalizedMessage();
        }
        map.put(AdminConstants.BASIC_IDENTIFICATION_SUCCESS, flag);
        map.put(AdminConstants.BASIC_IDENTIFICATION_MESSAGE, msg);
        DynamicDataSource.clearDataSource();
        return map;
    }

    @Override
    public Map<String, Object> verificationVerificationCode(String referenceCode,String code) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        Map<String, Object> map = new HashMap<String, Object>(2);
        String msg = "";
        boolean flag = false;
        try {
            List<ThirdSmsCode>smsCodes=thirdSmsCodeMapper.findByAttr("reference_code='"+referenceCode+"' and is_used=0 and expiry_time > "+System.currentTimeMillis(),null);
            if (!smsCodes.isEmpty()){
                ThirdSmsCode smsCode=smsCodes.get(0);
                if (code.equals(smsCode.getCode())){
                    flag=true;
                    smsCode.setIsUsed(BaseConstants.YES);
                    smsCode.setGmtModified(new Date());
                    thirdSmsCodeMapper.updateByPrimaryKey(smsCode);
                }else{
                    msg="验证码错误,请重新输入";
                }
            }else{
                msg="验证码不存在或失效，请重新获取";
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getLocalizedMessage();
        }
        map.put(AdminConstants.BASIC_IDENTIFICATION_SUCCESS, flag);
        map.put(AdminConstants.BASIC_IDENTIFICATION_MESSAGE, msg);
        DynamicDataSource.clearDataSource();
        return map;
    }

}
